package com.Controller.servlet;

import com.Model.Date.DateTime;
import com.Model.Pojo.Users;
import com.sql.DAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class CunQianQvQian extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");//编码格式
    	response.setCharacterEncoding("UTF-8");

		String type = request.getParameter("type"); //判断用户点击了哪个from的标志
    	
		int cunmoney = 0; //存款或者取款的金额
		String str; //存款或者取款的字符串
		String style;
		String style1;
		String source; //来源备注信息
    	if(type.equals("f1")){
    		str = (String)request.getParameter("cunkuan");
    		cunmoney = Integer.parseInt(str);
    		style = "1";
    		style1 = "存款";
    		source = "存入余额";
    	}else{
    		str = (String)request.getParameter("qvkuan");
    		cunmoney = Integer.parseInt(str);
    		cunmoney *= -1;
    		style = "0";
    		style1 = "取款";
    		source = "余额取出";
    	}
		
    	Users usermoney = new Users();
		HttpSession session = request.getSession();
		usermoney = (Users)session.getAttribute("users");//获取session中的用户
		
		int money = usermoney.getYue();//获取用户的账户余额
		int cash = usermoney.getMoney();//获取用户的现金
		money += cunmoney;
		cash -= cunmoney;
		DAO dao = new DAO();
		String usersname = usermoney.getZhanghao();
		String sql = "update users set yue='" + money + "' where name='" + usersname + "' ";
		dao.xiugai(sql);//更新users表中用户的账户余额
		sql = "update users set money='" + cash + "' where name='" + usersname + "' ";
		dao.xiugai(sql);//更新users表中用户的现金
		
		usermoney.setYue(money);
		usermoney.setMoney(cash);
		session.setAttribute("users", usermoney);
		
		if(style.equals("0")){
			cunmoney *= -1;
		}
		DateTime date = new DateTime();
		sql = "insert into cunkuanqvkuan (name,money,style,date,yue)value('" + usersname + "','" + cunmoney 
						+ "','" + style + "','" + date.getSettime() + "','" + money + "')";
		dao.zengjia(sql);
		
		sql = "insert into moneylist (name,style,value,date,cash,source)value('" + usersname + "','" + style1
		+ "','" + cunmoney + "','" + date.getSettime() + "','" + cash + "','" + source + "')";
		dao.zengjia(sql);
		
		response.sendRedirect("/Bank/WebPage/FrontPage/FinancialServicePage/DepositAndWithdrawal.jsp");
		
//		String type = request.getParameter("type"); //判断用户点击了哪个from的标志
//		String str; //存款或者取款的字符串
//		if(type.equals("f1")){
//			str = (String)request.getParameter("cunkuan");
//		}else{
//			str = (String)request.getParameter("qvkuan");
//		}
//		int cunmoney = 0; //存款或者取款的金额
//		cunmoney = Integer.parseInt(str);
//		Users usermoney = new Users();
//		HttpSession session = request.getSession();
//		usermoney = (Users)session.getAttribute("users");//获取session中的用户
//		CunKuanQvKuan ckqk = new CunKuanQvKuan();
//		ckqk.cunQv(usermoney.getZhanghao(), type, cunmoney);
	}
}
